﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SimpleSM
{
    public partial class CopyTypesFrom : Form
    {
        private int sessionid = 0;

        public CopyTypesFrom(int sessionid)
        {
            InitializeComponent();
            this.sessionid = sessionid;
        }

        private void btSelect_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                if (treeView1.SelectedNode.Name[0] == 'S')
                {
                    int source_sessionid = int.Parse(treeView1.SelectedNode.Name.Substring(1, treeView1.SelectedNode.Name.Length - 1));
                    if (source_sessionid == this.sessionid)
                    {
                        MessageBox.Show("Select difrent session.", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        return;
                    }
                    new SessionT().Copy(source_sessionid, this.sessionid);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Select session.", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            else
            {
                MessageBox.Show("Select session.", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void AddNodeTreeView(DataTable dt, DataRow dr, TreeNode node)
        {
            TreeNode node2;
            TreeNode node3;
            DataTable dts;
            foreach (DataRow dr2 in dt.Select("group_id = " + dr[0].ToString()))
            {
                node2 = node.Nodes.Add("G" + dr2[0].ToString(), dr2[1].ToString());
                this.AddNodeTreeView(dt, dr2, node2);
            }
            dts = new Session().GetA(int.Parse(dr[0].ToString()));
            foreach (DataRow rows in dts.Select())
            {
                node3 = node.Nodes.Add("S" + rows[0].ToString(), rows[1].ToString());
                node3.ImageKey = "computer";
                node3.SelectedImageKey = "computer";
            }
            if ((bool)dr[3])
            {
                node.Expand();
            }
        }

        [DllImport("shell32.dll", EntryPoint = "ExtractIcon")]
        extern static IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);

        static public Icon GetIcon(int index)
        {
            IntPtr Hicon = ExtractIcon(IntPtr.Zero, @"C:\WINDOWS\System32\shell32.dll", index);
            Icon icon = Icon.FromHandle(Hicon);
            return icon;
        }

        private void CopyTypesFrom_Load(object sender, EventArgs e)
        {
            imageList1.Images.Add("folder", GetIcon(126));
            imageList1.Images.Add("computer", GetIcon(15));
            TreeNode node;
            TreeNode node2;
            treeView1.Nodes.Clear();
            DataTable dt = new Group().Get();
            DataTable dts;
            foreach (DataRow row in dt.Select("group_id is null"))
            {
                node = treeView1.Nodes.Add("G" + row[0].ToString(), row[1].ToString());
                this.AddNodeTreeView(dt, row, node);
            }
            dts = new Session().GetA();
            foreach (DataRow rows in dts.Select())
            {
                node2 = treeView1.Nodes.Add("S" + rows[0].ToString(), rows[1].ToString());
                node2.ImageKey = "computer";
                node2.SelectedImageKey = "computer";
            }
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            btSelect_Click(sender, e);
        }
    }
}
